﻿using Microsoft.Azure;
using Microsoft.WindowsAzure.Management;
using Microsoft.WindowsAzure.Management.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Microsoft.Cloud.API.Azure.Services
{
    public class ManagementService : IDisposable
    {
        private readonly ManagementClient managementClient = null;

        public ManagementService(string subscriptionId)
        {
            this.managementClient = new ManagementClient(ADALService.GetCredential(subscriptionId), ADALService.ServiceManagementUrl);
        }

        public ManagementService(SubscriptionCloudCredentials credentials, Uri baseUri)
        {
            this.managementClient = new ManagementClient(credentials, baseUri);
        }

        /// <summary>
        /// get affinity groups
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <returns></returns>
        public async Task<IList<AffinityGroupListResponse.AffinityGroup>> GetAffinityGroupListAsync()
        {
            var response = await this.managementClient.AffinityGroups.ListAsync();

            return response.AffinityGroups;
        }

        /// <summary>
        /// get locations
        /// </summary>
        /// <returns></returns>
        public async Task<IList<LocationsListResponse.Location>> GetLocationListAsync()
        {
            var response = await this.managementClient.Locations.ListAsync();

            return response.Locations;
        }

        public void Dispose()
        {
            if (this.managementClient != null)
            {
                this.managementClient.Dispose();
            }
        }
    }
}